/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var getMinimumDifference = function (root) {
  const values = [];

  // 中序遍历收集节点值
  function inorder(node) {
    if (node === null) {
      return;
    }
    inorder(node.left);
    values.push(node.val);
    inorder(node.right);
  }

  inorder(root);

  // 计算相邻元素的最小差值
  let minDiff = Infinity;
  for (let i = 1; i < values.length; i++) {
    minDiff = Math.min(minDiff, values[i] - values[i - 1]);
  }

  return minDiff;
};